package com.voice.call.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.lang.model.element.Element;

import org.springframework.stereotype.Service;

import com.voice.call.common.Const;
import com.voice.call.entity.TaskIntegral;
import com.voice.call.entity.TaskLog;
import com.voice.call.mapper.TaskLogMapper;
import com.voice.call.service.TaskIntegralService;
import com.voice.call.service.TaskLogService;
import com.voice.call.utils.DateUtil;

@Service
public class TaskLogServiceImpl implements TaskLogService{
	
	@Resource
	private TaskLogMapper taskLogMapper;
	
	@Resource
	private TaskIntegralService taskIntegralService;
	
	
	//新增任务记录
	@Override
	public	boolean addTaskLog(TaskLog entity){
		
		if(entity==null){
			return false;
		}
		return taskLogMapper.insert(entity) >0? true:false;
			
	}
	
	//新增邀请任务记录、
	@Override
	public	boolean addInviteTaskLog(Integer user_id,String category){
		
		TaskIntegral task = taskIntegralService.getInviteCategory(category);
		return this.addInviteTaskLogForTask(user_id, task);
		
	}
	@Override
	public boolean addInviteTaskLogForTask(Integer user_id,TaskIntegral task){
		return this.addTaskLogForTask(user_id, task);
	}
	
	@Override
	public boolean addTaskLogForCategory(Integer user_id,String category){
		TaskIntegral task = taskIntegralService.getInviteCategory(category);
		return this.addTaskLogForTask(user_id, task);
	}
	
	@Override
	public boolean addTaskLogForTask(Integer user_id,TaskIntegral task){
		if(task!=null){
			TaskLog entity = new TaskLog();
			entity.setUserId(user_id);
			entity.setTaskId(task.getId());
			entity.setCategory(task.getCategory());
			entity.setIntegral(task.getIntegral());
			entity.setCtime(new Date());
			return this.addTaskLog(entity);
		}
		return false;
		
	}
	
	//判断当天是否已签到
	@Override
	public	boolean hasCheckIn(Integer user_id){
		
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.TASK_CHECK_IN);
		params.put("userId",user_id);
		params.put("startTime",DateUtil.getNowDateTimeString(DateUtil.DATE_FMT)+" 00:00:00");
		params.put("endTime",DateUtil.getNowDateTimeString(DateUtil.DATE_FMT)+" 23:59:59");
		
		int total = taskLogMapper.count(params);
		if (total >0) {
			return true;
		}
		return false;
	}

	//获取已签到的列表
	public  List<TaskLog> getCheckInList(Integer user_id){
		Date nowDate = new Date();
		
		Date firstDayTimeDate =  DateUtil.getFirstOfWeekDay(nowDate);
		Date lastDayTimeDate =  DateUtil.getLastOfWeekDay(nowDate);
		
		String firstDay = DateUtil.getDateTimeString(firstDayTimeDate, DateUtil.DATE_FMT)+" 00:00:00";
		String lastDay = DateUtil.getDateTimeString(lastDayTimeDate, DateUtil.DATE_FMT)+" 23:59:59";
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.TASK_CHECK_IN);
		params.put("userId",user_id);
		params.put("startTime",firstDay);
		params.put("endTime",lastDay);
		return taskLogMapper.list(params);
	}
	
	//获取lucky credit今天玩的次数
	@Override
	public	int getPlayLuckyCreditCount(Integer user_id){
		
		String dateStr = DateUtil.getNowDateTimeString(DateUtil.DATE_FMT);
		String startTime = dateStr+" 00:00:00";
		String endTime = dateStr+" 23:59:59";
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.TASK_LUCKY_CREDIT);
		params.put("userId",user_id);
		params.put("startTime",startTime);
		params.put("endTime",endTime);
		
		return taskLogMapper.count(params);
		
	}
	
	//判断某个类别记录是否存的总数
	@Override
	public	int getCategroyCount(Integer user_id,String category){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", category);
		params.put("userId",user_id);
		return taskLogMapper.count(params);
	}
	
	//获取连续签到次数
	@Override
	public	int getSuccessiveCheckInCount(Integer user_id){
		Date endDate = DateUtil.addDate(new Date(), -1);
		Date startDate = DateUtil.addDate(endDate, -5);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.TASK_CHECK_IN);
		params.put("userId",user_id);
		params.put("startTime",DateUtil.getDateTimeString(startDate, DateUtil.DATE_FMT) + " 00:00:00");
		params.put("endTime",DateUtil.getDateTimeString(endDate, DateUtil.DATE_FMT) + " 23:59:59");
		List<TaskLog> list=taskLogMapper.getSuccessiveCheckInList(params);
		if (list!=null && !list.isEmpty()) {
			
			int total = 0;
			String startTimeStr = DateUtil.getDateTimeString(endDate, DateUtil.DATE_FMT);
			for (TaskLog taskLog : list) {
				if (DateUtil.getDateTimeString(taskLog.getCtime(), DateUtil.DATE_FMT).equals(startTimeStr)) {
					total ++;
					endDate = DateUtil.addDate(endDate, -1);
					startTimeStr = DateUtil.getDateTimeString(endDate, DateUtil.DATE_FMT);
				}else {
					break;
				}
			}
			return total;
			
		}
		return 0;
	}
}
